package com.zhuoyue.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.zhuoyue.system.domain.TblDevice;
import com.zhuoyue.system.domain.dto.BaseDeviceMessage;
import com.zhuoyue.system.domain.dto.GroupRepDTO;
import com.zhuoyue.system.domain.dto.GroupReqDTO;
import com.zhuoyue.system.domain.vo.TblDeviceVo;
import com.zhuoyue.system.domain.vo.TblGatewayVo;
import com.zhuoyue.system.mapper.SysDeptMapper;
import com.zhuoyue.system.mapper.TblDeviceMapper;
import com.zhuoyue.system.mapper.TblGatewayMapper;
import com.zhuoyue.system.service.impl.TblGroupDeviceRelationService;
import com.zhuoyue.web.utils.OrgCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.system.domain.TblGroup;
import com.zhuoyue.system.service.ITblGroupService;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author wjming
 * @date 2021-09-06
 */
@RestController
@RequestMapping("/system/group")
public class TblGroupController extends BaseController {
    @Autowired
    private ITblGroupService tblGroupService;

    @Autowired
    private OrgCodeUtils orgCodeUtils;

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private TblGroupDeviceRelationService groupDeviceRelationService;
    @Autowired
    private TblDeviceMapper deviceMapper;
    @Autowired
    private TblGatewayMapper gatewayMapper;

    /**
     * 查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GroupReqDTO groupReq) {
        TblGroup tblGroup = new TblGroup().doToDto(groupReq);
        startPage();
        List<TblGroup> list = tblGroupService.selectTblGroupList(tblGroup);

        List<GroupRepDTO> groupReps = new ArrayList<>(10);
        list.forEach(l -> {
            GroupRepDTO groupRep = new GroupRepDTO().doToDto(l);
            groupReps.add(groupRep);
        });
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setRows(groupReps);
        return dataTable;
    }

    /**
     * 导出群组列表
     */
    @GetMapping("/export")
    public AjaxResult export(TblGroup tblGroup) {
        String homeId = "-1";
        List<TblGroup> list = tblGroupService.selectTblGroupList(tblGroup);
        List<GroupRepDTO> groupReps = new ArrayList<>(10);
        list.forEach(l -> {
            GroupRepDTO groupRep = new GroupRepDTO().doToDto(l);
            if (homeId.equals(l.getHomeId())) {
                TblGatewayVo gatewayVo = gatewayMapper.selectGatewayByGatewayId(null, l.getGatewayMac());
                groupRep.setHomeName(gatewayVo.getHomeName());
            }
            groupReps.add(groupRep);
        });
        ExcelUtil<GroupRepDTO> util = new ExcelUtil<GroupRepDTO>(GroupRepDTO.class);

        return util.exportExcel(groupReps, "群组");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        TblGroup tblGroup = tblGroupService.selectTblGroupById(id);
        GroupRepDTO groupRep = new GroupRepDTO().doToDto(tblGroup);
        List<String> list = groupDeviceRelationService.selectByGroupKey(tblGroup.getId());
        if (!CollectionUtils.isEmpty(list)) {
            List<TblDeviceVo> listDeviceByIds = deviceMapper.getListDeviceByIds(list);
            List<BaseDeviceMessage> maps = new ArrayList<>(10);
            listDeviceByIds.forEach(l -> {
                BaseDeviceMessage baseDeviceMessage = new BaseDeviceMessage(l.getDeviceName(), l.getDeviceType(), l.getDefaultDeviceName(), l.getDeviceMac());
                maps.add(baseDeviceMessage);
            });
            groupRep.setDeviceList(maps);
        }


        //查询设备信息
        return AjaxResult.success(groupRep);
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "群组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TblGroup tblGroup) {
        return toAjax(tblGroupService.insertTblGroup(tblGroup));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "群组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TblGroup tblGroup) {
        return toAjax(tblGroupService.updateTblGroup(tblGroup));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "群组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tblGroupService.deleteTblGroupByIds(ids));
    }
}
